#!/usr/bin/perl -w
########################################################################
# script name:  ssh-to.pl
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/02/21    0.1     MDUMONT         initial
########################################################################
package OPMGT::Ssh;

use strict;
use warnings;
use Net::SSH2;
#BEGIN {
#	use Exporter ();
#	our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
#	
#	# set the version for version checking
#	$VERSION = 1.00;
#	# if using RCS/CVS, this may be preferred
#	$VERSION = sprintf "%d.%03d", q$Revision: 1.1 $ =~ /(\d+)/g;
#	
#	@ISA = qw(Exporter);
#	@EXPORT = qw(&file_to_scp &cmd_to_ssh &deploy_pub_key);
#	%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
#	
#	# your exported package globals go here,
#	# as well as any optionally exported functions
#	@EXPORT_OK = qw(&file_to_scp &cmd_to_ssh &deploy_pub_key);
#	#@EXPORT_OK = qw($Var1 %Hashit &func3);
#}
#our @EXPORT_OK;
#######################################
# MAIN
#######################################
#######################################
# Test locally
#######################################
#my $s_user = $ARGV[0];
#my $s_pubkey = $ARGV[1];
#my $s_privkey = $ARGV[2];
#my $s_host = $ARGV[3];
#my $s_command = $ARGV[4];
#my $s_source_file = $ARGV[4];
#my $s_dest_file = $ARGV[5];
#######################################
#######################################
sub file_to_scp
{
	my $s_user = shift;
	my $s_pubkey = shift;
	my $s_privkey = shift;
	my $s_host = shift;
	my $s_source_file = shift;
	my $s_dest_file = shift;
	###########################################################
	# Order: method, connect, authenticate, channels (commands)
	# Set Debug
	# $ssh2->debug(1);
	###########################################################
	my $ssh2 = Net::SSH2->new();
	$ssh2->connect("$s_host") or die "Unable to connect Host $@ \n";
		print "Copying \"$s_source_file\" to \"$s_dest_file\" on \"$s_host\"\n";
		$ssh2->auth_publickey( $s_user, $s_pubkey, $s_privkey ) or die $!;
                $ssh2->scp_put($s_source_file, $s_dest_file);
}
sub cmd_to_ssh
{
	my $s_user = shift;
	my $s_pubkey = shift;
	my $s_privkey = shift;
	my $s_host = shift;
	my $s_command = shift;
	###########################################################
	# Order: method, connect, authenticate, channels (commands)
	# Set Debug
	# $ssh2->debug(1);
	###########################################################
	my $ssh2 = Net::SSH2->new();
	$ssh2->connect("$s_host") or die "Unable to connect Host $@ \n";
		#print "Executing \"$s_command\" on \"$s_host\"\n";
		$ssh2->auth_publickey( $s_user, $s_pubkey, $s_privkey ) or die "Unable to authenticate with $@ \n";
		my $chan = $ssh2->channel();
		$chan->blocking(1);
		$chan->exec("$s_command");
		while (<$chan>){ print } 
		$chan->close();
}
sub deploy_pub_key
{
	my $s_user = shift;
	my $s_pubkey = shift;
	my $s_host = shift;
	my $s_user_passwd = shift;
	#######################################
	# Ask Password and keep it for the loop
	#######################################
	print "\nCopying key $s_pubkey to .ssh/authorized_keys on $s_user\@$s_host\n";
	my $ssh2 = Net::SSH2->new();
	$ssh2->connect("$s_host") or die "Unable to connect Host $@ \n";
	$ssh2->auth_password( $s_user, $s_user_passwd ) or die $!;
	$ssh2->scp_put($s_pubkey, "/tmp/.tmpkey") or die $!;
	################################################################################################################
	# file_to_scp example...for reference
	# OPMGT::Ssh::file_to_scp($OPMSSHUSER, $OPMSSHKEYPUB, $OPMSSHKEYPRV, $s_runserver, $OPMSSHKEYPUB, "/tmp/tmpkey");
	################################################################################################################
	my $command = "test -d .ssh || mkdir -m 700 .ssh ; cat /tmp/.tmpkey >> .ssh/authorized_keys ; chmod 600 .ssh/authorized_keys";
	$ssh2 = Net::SSH2->new();
	$ssh2->connect("$s_host") or die "Unable to connect Host $@ \n";
	########################################################
	# debug
	# print "Server: \"$s_host\" \nCommand: \"$command\"\n";
	########################################################
	$ssh2->auth_password( $s_user, $s_user_passwd ) or die $!;
	my $chan = $ssh2->channel();
		$chan->blocking(1);
		$chan->exec("$command");
		while (<$chan>){ print }
		$chan->close();
}
1;
